﻿package com.snake.core{
	import flash.geom.Point;
	import flash.events.Event;
	public class SnakeInstance extends SnakeBlockMC{
		public var cur_dir:String="left";
		public var out=false;
		public var index=0;
		public var Blocks_Amount:int=0;

		private var Blocks:Array = new Array();
		private var path:Array = new Array();
		private var c:int=0;
		public function SnakeInstance(sx, sy, start_blocks) {
			try {
				index=Game.map.getTileIndex(new Point(sx,sy));
				var centre=Game.map.getCentreTile(Game.map.getTileIndex(new Point(sx,sy)));

				this.x=centre.x;
				this.y=centre.y;
				Game.main_stage.addChild(this);
				Game.map.setTile(Game.map.getTileIndex(new Point(sx,sy)), "blank", false);

				for (var i = 0; i < start_blocks; i++) {
					addBlock();
				}
				path=snake_tiles();
				c=path.length;
			} catch (e) {
				trace("Error in SnakeInstance().<init>()");
			}
		}
		public function addBlock() {
			var d:Point = new Point();
			var tile:int=0;

			try {
				tile=Game.map.getTileIndex(new Point(path[c-Blocks.length].x,path[c-Blocks.length].y));
			} catch (e) {
				tile=index;
			}

			Blocks[Blocks_Amount]=new BlockInstance();
			Blocks[Blocks_Amount].x=Game.map.getCentreTile(tile).x;
			Blocks[Blocks_Amount].y=Game.map.getCentreTile(tile).y;
			Blocks[Blocks_Amount].index=Game.map.getTileIndex(new Point(Blocks[Blocks_Amount].x,Blocks[Blocks_Amount].y));

			var bI:int=Game.map.getTileIndex(new Point(Blocks[Blocks_Amount].x,Blocks[Blocks_Amount].y));
			Game.map.setTile(bI, "blank", false);
			Game.main_stage.addChild(Blocks[Blocks_Amount]);

			Blocks_Amount++;
		}
		private function snake_tiles() {
			var tempArr:Array = new Array();

			tempArr[0] = new Tile();
			tempArr[0].x=this.x;
			tempArr[0].y=this.y;
			tempArr[0].index=index;

			for (var i = 1; i <= Blocks.length; i++) {
				tempArr[i] = new Tile();
				tempArr[i].x=Blocks[i-1].x;
				tempArr[i].y=Blocks[i-1].y;
				tempArr[i].index=Blocks[i-1].index;
			}
			return tempArr;
		}
		public function move(dir) {
			try {
				out=false;

				switch (dir) {
					case "left" :
						this.x=Game.map.getCentreTile(index-Constants.JUMP).x;
						this.y=Game.map.getCentreTile(index).y;
						index-=Constants.JUMP;
						break;
					case "right" :
						this.x=Game.map.getCentreTile(index+Constants.JUMP).x;
						this.y=Game.map.getCentreTile(index).y;
						index+=Constants.JUMP;
						break;
					case "up" :
						this.x=Game.map.getCentreTile(index).x;
						this.y=Game.map.getCentreTile(index-1).y;
						index-=1;
						break;
					case "down" :
						this.x=Game.map.getCentreTile(index).x;
						this.y=Game.map.getCentreTile(index+1).y;
						index+=1;
						break;
				}
				cur_dir=dir;
				path[c] = new Tile();
				path[c].x=this.x;
				path[c].y=this.y;
				path[c].index=index;
				
				for (var i:int = 0; i < Blocks.length; i++) {
					Blocks[i].x=path[c-(i+1)].x;
					Blocks[i].y=path[c-(i+1)].y;
					Game.map.setTile(path[c-(i+1)].index,"blank",false);
				}
				if (Geom.distance(path[c],path[c-1])>20) {
					out=true;
					return;
				}
				Game.map.setTile(path[c-(Blocks.length+1)].index,"blank",true);
				c++;
			} catch (e) {
				trace("Error in SnakeInstance.move()");
				out=true;
			}
		}
		public function destroy() {
			for (var i = 0; i < Blocks.length; i++) {
				Blocks[i].destroy();
			}
			Game.main_stage.removeChild(this);
		}
	}
}